﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SpringStudio.Common;
using SpringStudio.DAL;


namespace Sdepci.InfoCode.BLL.CodeDesign
{
    /// <summary>
    /// 简要信息码管理类
    /// </summary>
    public class BriefManager
    {
        /// <summary>
        /// 保存简要信息码
        /// </summary>
        /// <param name="briefCode">编码</param>
        /// <param name="briefDesc">中文说明</param>
        /// <param name="ruleGuid">编码类型Guid</param>
        /// <param name="createUserId">创建用户证档号</param>
        /// <param name="serialReference">流水号参考码</param>
        /// <param name="serialNumber">当前流水号值</param>
        /// <returns></returns>
        public bool Save(string briefCode, string briefDesc, string ruleGuid, string createUserId, string serialReference, string serialNumber)
        {
            try
            {
                string sql = string.Empty;

                // 先判断是否已有同类型，并且同流水号参考码的简要信息
                sql = "Select BriefGuid,SerialNumber From InfoCodeBrief Where RuleGuid=@RuleGuid And SerialReference=@SerialReference";
                DataSet ds = SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid),
                    SqlHelper.NewSqlParameter("@SerialReference", serialReference));
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    // 再判断已有的同类型、同流水号参考码所存储的流水号是否小于当前流水号
                    if (int.Parse(ds.Tables[0].Rows[0]["SerialNumber"].ToString()) < int.Parse(serialNumber))
                    {
                        // 更新简要信息记录
                        sql = "Update InfoCodeBrief Set BriefCode=@BriefCode,BriefDesc=@BriefDesc,CreateTime=@CreateTime,CreateUserId=@CreateUserId,SerialNumber=@SerialNumber Where RuleGuid=@RuleGuid And SerialReference=@SerialReference";
                        SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                            SqlHelper.NewSqlParameter("@BriefCode", briefCode),
                            SqlHelper.NewSqlParameter("@BriefDesc", briefDesc),
                            SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid),
                            SqlHelper.NewSqlParameter("@CreateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                            SqlHelper.NewSqlParameter("@CreateUserId", createUserId),
                            SqlHelper.NewSqlParameter("@SerialReference", serialReference),
                            SqlHelper.NewSqlParameter("@SerialNumber", serialNumber));
                    }
                }
                else
                {
                    // 创建新的简要信息记录
                    string briefGuid = Guid.NewGuid().ToString(); // 创建一个新的Guid
                    sql = "Insert Into InfoCodeBrief (BriefGuid,BriefCode,BriefDesc,RuleGuid,CreateTime,CreateUserId,SerialReference,SerialNumber) Values(@BriefGuid,@BriefCode,@BriefDesc,@RuleGuid,@CreateTime,@CreateUserId,@SerialReference,@SerialNumber)";
                    SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                        SqlHelper.NewSqlParameter("@BriefGuid", briefGuid),
                        SqlHelper.NewSqlParameter("@BriefCode", briefCode),
                        SqlHelper.NewSqlParameter("@BriefDesc", briefDesc),
                        SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid),
                        SqlHelper.NewSqlParameter("@CreateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                        SqlHelper.NewSqlParameter("@CreateUserId", createUserId),
                        SqlHelper.NewSqlParameter("@SerialReference", serialReference),
                        SqlHelper.NewSqlParameter("@SerialNumber", serialNumber));
                }

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 获取某一类型的简要信息列表（数据集）
        /// </summary>
        /// <param name="ruleGuid">编码类型Guid</param>
        /// <returns></returns>
        public DataSet GetBriefDs(string ruleGuid)
        {
            try
            {
                string sql = string.Empty;
                sql = "Select * From InfoCodeBrief Where RuleGuid=@RuleGuid";
                return SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid));
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return null;
            }
        }

        /// <summary>
        /// 生成某一类型的编码新的流水号
        /// </summary>
        /// <param name="ruleGuid">编码类型Guid</param>
        /// <param name="serialReference">流水号参考码</param>
        /// <param name="serialLength">流水号长度</param>
        /// <param name="serialStart">流水号起始编码</param>
        /// <returns></returns>
        public string CreateSerialNumber(string ruleGuid, string serialReference, string serialLength, string serialStart)
        {
            try
            {
                if (serialStart == "")
                {
                    serialStart = "1";
                }

                string serial = string.Empty;  // 初始化流水号字符串
                int number = 0;  // 初始化整数类型的流水号

                // 首先查找简要信息表中是否已有相同的流水号参考码
                string sql = string.Empty;
                sql = "Select SerialNumber From InfoCodeBrief Where RuleGuid=@RuleGuid And SerialReference=@SerialReference";
                DataSet ds = SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid),
                    SqlHelper.NewSqlParameter("@SerialReference", serialReference));
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    number = int.Parse(ds.Tables[0].Rows[0]["SerialNumber"].ToString()) + 1;
                }
                else
                {
                    number = int.Parse(serialStart);
                }

                serial = number.ToString();

                // 假如流水号限定长度
                if (serialLength != "0")
                {
                    int currentLength = serial.Length;
                    int limitLength = int.Parse(serialLength);

                    for (int i = currentLength; i < limitLength; i++)
                    {
                        serial = "0" + serial;
                    }
                }

                return serial;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return null;
            }
        }
    }
}
